package com.keste.ojt1.model;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless(name = "Task11SessionEJB", mappedName = "OJT1-sbadampudiTask11Model-Task11SessionEJB")
public class Task11SessionEJBBean implements Task11SessionEJBLocal {
    @PersistenceContext(unitName="sbadampudiTask11Model")
    private EntityManager em;

    public Task11SessionEJBBean() {
    }

    public Object queryByRange(String jpqlStmt, int firstResult,
                               int maxResults) {
        Query query = em.createQuery(jpqlStmt);
        if (firstResult > 0) {
            query = query.setFirstResult(firstResult);
        }
        if (maxResults > 0) {
            query = query.setMaxResults(maxResults);
        }
        return query.getResultList();
    }

    public Employees persistEmployees(Employees employees) {
        em.persist(employees);
        return employees;
    }

    public Employees mergeEmployees(Employees employees) {
        return em.merge(employees);
    }

    public void removeEmployees(Employees employees) {
        employees = em.find(Employees.class, employees.getEmployeeId());
        em.remove(employees);
    }

    /** <code>select o from Employees o</code> */
    public List<Employees> getEmployeesFindAll() {
        return em.createNamedQuery("Employees.findAll").getResultList();
    }

    public Departments persistDepartments(Departments departments) {
        em.persist(departments);
        return departments;
    }

    public Departments mergeDepartments(Departments departments) {
        return em.merge(departments);
    }

    public void removeDepartments(Departments departments) {
        departments = em.find(Departments.class, departments.getDepartmentId());
        em.remove(departments);
    }

    /** <code>select o from Departments o</code> */
    public List<Departments> getDepartmentsFindAll() {
        return em.createNamedQuery("Departments.findAll").getResultList();
    }
  /*public List<Employees> getEmployeesFindByName(Object firstName, Object lastName){
            List<Employees> returnList = new ArrayList<Employees>();
            Query q =  em.createNamedQuery("Employees.findByName");
            q.setParameter("bindFirstName", firstName);
            q.setParameter("bindLastName", lastName);
            if(q.getResultList().size() > 0)
                returnList = q.getResultList();
                      return returnList;
      }*/

    
    /** <code>select o from Employees o where o.firstName like :FirstName and o.lastName like :LastName</code> */
    public List<Employees> getEmployeesFindByName(Object FirstName,
                                                  Object LastName) {
        return em.createNamedQuery("Employees.findByName").setParameter("FirstName", FirstName).setParameter("LastName", LastName).getResultList();
    }
}
